import fetchHttp from "@/fetch";

const validateAccount = async (rule, value, callback) => {
  const reg = /^[a-zA-Z][0-9a-zA-Z_]{3,15}$/;
  if (!reg.test(value)) return callback(new Error('账号只能包含英文字母、数字和下划线，且必须以英文字母开头，长度为4~16位'));
  const isExisted = await fetchHttp.get(`/users/exist/${value}`, null, false, false);
  if (isExisted) return callback(new Error('圆滚滚的账号已存在！'));
  callback();
}

export default {
  account: [{
      required: true,
      message: '请输入圆滚滚的账号',
      trigger: 'blur'
    },
    {
      validator: validateAccount,
      trigger: 'blur'
    }
  ],
  password: [{
      required: true,
      message: '请输入圆滚滚的密码',
      trigger: 'blur'
    },
    {
      min: 3,
      max: 20,
      message: '圆滚滚的密码长度为3~20位',
      trigger: 'blur'
    }
  ],
  confirmPassword: [{
      required: true,
      message: '请把圆滚滚的密码再输入一遍',
      trigger: 'blur'
    },
    {
      min: 3,
      max: 20,
      message: '圆滚滚的密码长度为3~20位',
      trigger: 'blur'
    }
  ],
  nickname: {
    required: true,
    message: '请输入圆滚滚的昵称',
    trigger: 'blur'
  },
  realName: [{
      required: true,
      message: '请输入圆滚滚的真名',
      trigger: 'blur'
    },
    {
      min: 1,
      max: 20,
      message: '圆滚滚的真名长度为1~20位',
      trigger: 'blur'
    }
  ],
  role: {
    required: true,
    message: '请选择圆滚滚的角色',
    trigger: 'change'
  }
}
